package com.sicau.smart_farmer.controller;

import com.sicau.smart_farmer.common.CommonConstants;
import com.sicau.smart_farmer.common.ResultVO;
import com.sicau.smart_farmer.entity.TrainingBatch;
import com.sicau.smart_farmer.entity.User;
import com.sicau.smart_farmer.entity.UserTrainingRecord;
import com.sicau.smart_farmer.service.TrainingBatchService;
import com.sicau.smart_farmer.service.UserService;
import com.sicau.smart_farmer.service.UserTrainingRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "批次")
@Slf4j
@Controller
@RequestMapping(CommonConstants.TRAINING_PREFIX)
public class TrainingController {

    @Autowired
    private TrainingBatchService trainingBatchService;

    @Autowired
    private UserTrainingRecordService userTrainingRecordService;

    @ApiOperation(value = "新增培训批次")
    @ApiImplicitParam(name = "trainingBatch",required = true,value = "培训批次的基本信息",dataType = "TrainingBatch")
    @PostMapping(value = "addTrainingBatch")
    @ResponseBody
    public ResultVO addTrainingBatch(@RequestBody TrainingBatch trainingBatch){
        boolean res = trainingBatchService.addTrainingBatch(trainingBatch);
        if (!res){
            return ResultVO.FAILED();
        }

        return ResultVO.SUCCESS();
    }

    @ApiOperation(value = "修改培训批次")
    @ApiImplicitParam(name = "trainingBatch",required = true,value = "被修改后培训批次的基本信息，必须所有信息全部重新上传")
    @PostMapping(value = "updateTrainingBatch")
    @ResponseBody
    public ResultVO updateTrainingBatch(@RequestBody TrainingBatch trainingBatch){
        TrainingBatch updateTrainingBatch = trainingBatchService.updateTrainingBatch(trainingBatch);
        if (updateTrainingBatch==null){
            return ResultVO.FAILED("请检查培训批次id是否正确");
        }
        return ResultVO.SUCCESS(updateTrainingBatch);
    }

    @ApiOperation(value = "根据id删除培训批次")
    @ApiImplicitParam(name = "trainingBatchIdList",required = true,value ="培训批次的id")
    @GetMapping(value = "deleteTrainingBatch")
    @ResponseBody
    public ResultVO deleteTrainingBatch(@RequestParam("trainingBatchIdList") List<String> trainingBatchIdList){
        for (String trainingBatchId : trainingBatchIdList){
            trainingBatchService.deleteTrainingBath(trainingBatchId);
        }
        return ResultVO.SUCCESS();
    }

    @ApiOperation(value = "获取所有的批次")
    @GetMapping(value = "getAllTrainingBatch")
    @ResponseBody
    public ResultVO getAllTrainingBatch(@ApiIgnore HttpSession session){
        List<TrainingBatch> allTrainingBatch = trainingBatchService.getAllTrainingBatch();
       //添加培训批次信息到session
        for(TrainingBatch t : allTrainingBatch){
            session.setAttribute(t.getId(),t);
        }
        return ResultVO.SUCCESS(allTrainingBatch).setCount(allTrainingBatch.size());
    }

    //------------------------trainingBatch相关↑↑↑     userTrainingRecord相关↓↓↓----------------------------


    @Autowired
    private HttpSession session;

    @ApiOperation(value = "查询用户参与培训批次的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",required = true,value = "0:根据用户id查询;1:根据培训批次id查询"),
            @ApiImplicitParam(name = "id",required = true,value = "对应type的id")
    })
    @GetMapping(value = "selectUserTrainingRecord/{type}")
    @ResponseBody
    public ResultVO selectUserTrainingRecord(String id, @PathVariable("type") String type ) {
        List<UserTrainingRecord> list ;
        switch (type) {
            case "0"://根据用户id查询
                list = userTrainingRecordService.selectUserTrainingRecordByUserId(id);
                break;
            case "1"://根据培训批次id查询
                list = userTrainingRecordService.selectUserTrainingRecordByTrainingBatchId(id);
                break;
            default://不符合情况
                return ResultVO.FAILED("type出错");
        }
        ArrayList<User> users = new ArrayList<>();

        System.out.println(session.getId());

        for(UserTrainingRecord t : list){
            User user = t.getUser();
            users.add(user);
            session.setAttribute(user.getId(),user);//将查询数据保存到session
        }
        return ResultVO.SUCCESS(users).setOtherData(list).setCount(users.size());
    }

    @ApiOperation(value = "根据培训批次id和用户培训状态查询用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "trainingBatchId",required = true,value = "培训批次的id"),
            @ApiImplicitParam(name = "process",required = true,value = "0：正在培训  1：未通过  2：已完成")
    })
    @GetMapping(value = "trainingBatchRecordTrainingBatchIdByProcess")
    @ResponseBody
    public ResultVO trainingBatchRecordTrainingBatchIdByProcess(String trainingBatchId,String process){
        List<UserTrainingRecord> trainingBatchRecordTrainingBatchIdByProcess = userTrainingRecordService.getTrainingBatchRecordTrainingBatchIdByProcess(trainingBatchId, process);
        ArrayList<User> users = new ArrayList<>();

        for(UserTrainingRecord t : trainingBatchRecordTrainingBatchIdByProcess){
            User user = t.getUser();
            users.add(user);
            session.setAttribute(user.getId(),user);//将查询数据保存到session
        }

        return ResultVO.SUCCESS(users).setOtherData(trainingBatchRecordTrainingBatchIdByProcess).setCount(users.size());
    }

    @ApiOperation(value = "新增用户培训的记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userIdList",required = true,value = "用户id数组"),
            @ApiImplicitParam(name = "trainingBatchId",required = true,value = "培训批次的id")
    })
    @PostMapping(value = "addUserTrainingRecord")
    @ResponseBody
    public ResultVO addUserTrainingRecord(@RequestParam("userIdList") List<String> userIdList, String trainingBatchId){
        for (String userId:userIdList){
            System.out.println(userId);
            userTrainingRecordService.addUserTrainingRecord(userId, trainingBatchId);
        }
        return ResultVO.SUCCESS();
    }

    @ApiOperation(value = "删除用户培训的记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userTrainingRecordIdList",required = true,value = "String数组")
    })
    @GetMapping(value = "deleteUserTrainingRecord")
    @ResponseBody
    /**
     * [
     *
     * ]
     */
    public ResultVO deleteUserTrainingRecord(@RequestParam List<String> userTrainingRecordIdList){
        for (String item: userTrainingRecordIdList) {
            userTrainingRecordService.deleteUserTrainingRecordById(item);
        }
        return ResultVO.SUCCESS();
    }

    @ApiOperation(value = "更新批次的用户状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userTrainingRecordId",required = true,value = "userTrainingRecord的id"),
            @ApiImplicitParam(name = "process",required = true,value = "0：正在培训  1：未通过  2：已完成")
    })
    @PostMapping(value = "updateUserTrainingRecordProcess")
    @ResponseBody
    public ResultVO updateUserTrainingRecordProcess(String userTrainingRecordId , String process){
        boolean res = userTrainingRecordService.updateUserTrainingRecordProcess(userTrainingRecordId, process);
        if (!res){
            return ResultVO.FAILED();
        }
        return ResultVO.SUCCESS();
    }



    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取所有没有参加某批次的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "batchId",required = true,value = "某批次的id"),
    })
    @RequestMapping(value = "getNotInTrainingBatch",method = RequestMethod.GET)
    @ResponseBody
    public ResultVO getNotInTrainingBatch(String batchId){


        List<UserTrainingRecord> userTrainingRecords = userTrainingRecordService.selectUserTrainingRecordByTrainingBatchId(batchId);
        ArrayList<String > userIds = new ArrayList<>();
        for (UserTrainingRecord u : userTrainingRecords){
            userIds.add(u.getUser().getId());
        }

        List<User> users = userService.queryUsersByIdNotIn(userIds);

        return ResultVO.SUCCESS(users);

    }

}
